package com.beiing.tianshuai.tianshuai.itemtouch;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

/**
 * Created by Jinni on 2017/11/1 0001
 */

public class DefaultItemTouchHelperCallback extends ItemTouchHelper.Callback {

    /**
     * item操作的回调
     */
    private OnItemTouchCallbackListener mOnItemTouchCallbackListener;

    /**
     * 是否可以拖拽
     */
    private boolean isCanDrag = false;

    /**
     * 是否可以滑动
     */
    private boolean isCanSwipe = false;

    public DefaultItemTouchHelperCallback(OnItemTouchCallbackListener onItemTouchCallbackListener) {
        this.mOnItemTouchCallbackListener = onItemTouchCallbackListener;
    }

    /**
     * 设置item的操作回调去更新ui
     *
     * @param onItemTouchCallbackListener onTouch操作回调
     */
    public void setOnItemTouchCallbackListener(OnItemTouchCallbackListener onItemTouchCallbackListener) {
        this.mOnItemTouchCallbackListener = onItemTouchCallbackListener;
    }

    /**
     * 设置是否可以拖拽
     *
     * @param canDrag true = 可以 | false = 禁止
     */
    public void setDragEnable(boolean canDrag) {
        isCanDrag = canDrag;
    }

    /**
     * 设置是否可以滑动
     *
     * @param canSwipe true = 可以 | false = 禁止
     */
    public void setSwipeEnable(boolean canSwipe) {
        isCanSwipe = canSwipe;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return isCanDrag;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return isCanSwipe;
    }

    /**
     * 当用户拖拽或滑动item的时候我们需要告诉系统滑动或拖拽的方向
     *
     * @param recyclerView 列表视图
     * @param viewHolder   列表视图的Holder
     * @return 方向
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            // flag如果值是0相当于这个功能被关闭
            int dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            int swipeFlag = 0;
            return makeMovementFlags(dragFlag, swipeFlag);
        } else if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
            int orientation = linearLayoutManager.getOrientation();

            int dragFlag = 0; // 允许拖拽的方向
            int swipeFlag = 0; // 允许滑动的方向

            // 当RecyclerView的线性布局横纵向不同时所做的相应是不同的
            switch (orientation) {
                case LinearLayoutManager.HORIZONTAL:// 如果是横向布局
                    dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                    swipeFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                    break;
                case LinearLayoutManager.VERTICAL:
                    dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                    swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                    break;
            }
            return makeMovementFlags(dragFlag, swipeFlag);
        }
        return 0;
    }

    /**
     * 当item被拖拽的时候被回调
     *
     * @param recyclerView     RecyclerView
     * @param srcViewHolder    被拖拽的item
     * @param targetViewHolder 目标item
     * @return 移动结果
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder srcViewHolder, RecyclerView.ViewHolder targetViewHolder) {
        return mOnItemTouchCallbackListener != null && mOnItemTouchCallbackListener.onMove(srcViewHolder.getAdapterPosition(), targetViewHolder.getAdapterPosition());
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        if (mOnItemTouchCallbackListener != null) {
            mOnItemTouchCallbackListener.onSwiped(viewHolder.getAdapterPosition());
        }
    }

    public interface OnItemTouchCallbackListener {
        /**
         * 当某个Item被滑动删除的时候回调接口
         *
         * @param adapterPosition item position
         */
        void onSwiped(int adapterPosition);

        /**
         * 当两个Item位置互换的时候回调接口
         *
         * @param srcPosition    被拖拽的item position
         * @param targetPosition 目标item position
         * @return true = 处理了相应的操作，false = 未处理
         */
        boolean onMove(int srcPosition, int targetPosition);
    }
}
